package dao;

import pojo.Cart;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @ClassName CartSql
 * @Author dhn
 * @date 2020.12.19 09:52
 */
public class CartSql {
    public static void addCart(Cart cart) {
        String sql = "insert into cart value(null,'"+cart.getCommodity_name()+"','"+cart.getCommodity_price()+"','"+cart.getCommodity_img()+"')";
        System.out.println(sql);
        JDBCconection.updata(sql);
    }

    public static List<Cart> queryCart() throws SQLException {
        List<Cart> cartList = new ArrayList<>();
        String sql = "select * from cart";
        ResultSet rs = JDBCconection.getData(sql);
        Cart cart = null;
        while (rs.next()){
            cart=new Cart();
            cart.setId(rs.getInt(1));
            cart.setCommodity_name(rs.getString(2));
            cart.setCommodity_price(Double.parseDouble(rs.getString(3)));
            cart.setCommodity_img(rs.getString(4));
            cartList.add(cart);
        }
        return cartList;
    }

    public static void deleteCartById(int id) {
        String sql = "delete from cart where id='"+id+"'";
        JDBCconection.updata(sql);
    }

    public static int queryCartNum() throws SQLException {
        String sql = "select count(*) totalNum from cart";
        ResultSet rs = JDBCconection.getData(sql);
        int num = 0;
        if (rs.next()){
            num = rs.getInt("totalNum");
        }
        return num;
    }
}
